package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.*;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds,@RequestBody Role role){
        try {
            roleService.add(permissionIds,menuIds,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if(role!=null){
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    public List<Integer> findPermissionIdsByRoleId(Integer id){
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
        return permissionIds;
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public List<Integer> findMenuIdsByRoleId(Integer id){
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        return menuIds;
    }

    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody Role role,Integer [] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e){
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }


    @RequestMapping(value = "/findAll")
    public Result findAll(){
        List<CheckItem> list = roleService.findAll();
        if(list!=null && list.size()>0){
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        }else{
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findAllForMenu")
    public Result findAllForMenu(){
        List<Menu> list = roleService.findAllForMenu();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
    }
}
